%% How to use this script
% Inputs need to be:
% mainDir = the directory with all the folder images
% iValues = the index values of the folders you wish to look at. Do
%   dir(mainDir) to determine what values they should be
% infoStorage = where you want the excel sheets and images to go. You must
% place them in a new folder before running another test

mainDir = 'D:\Mac Pig GUI Project\Image Sets\Multispect Patients';
% iValues = 3:length(mainDir);
iValues = 3;
infoStorage = 'D:\Mac Pig GUI Project\Algorithm Results\Multispect Patients';
centersExcelName = 'Full Fovea and Mac Radius Data.xls';

%%
%Generate list of all folders to loop through
patientFolders = dir(mainDir);
numFiles = size(patientFolders, 1);
dataCenter = {'Patient ID', 'fovRow', 'fovCol', 'macRadius', 'opDiskRow', 'opDiskCol', 'opDiskRadius'};
%Loop through directories
for i = iValues; %Change to number of patients
    % Read in directory of image
    blueImgFiles = [mainDir filesep sprintf('%s', patientFolders(i).name) filesep 'BLUE'];
    
    %Pre-op blue image
    blueAvgIm = computeDirAvgImage(blueImgFiles, '*.tif', 'dir');
    
    % Find approximate center of fovea with box_cent_fov and blue image.
    [fovRow, fovCol] = box_cent_fov(blueAvgIm);
    close;
    
    % Highlight a larger region that was selected by box_cent_fov so user sees
    temp = blueAvgIm;
    temp(fovRow-2:fovRow+2,fovCol-2:fovCol+2) = 1;
    
    % Maximize figure size and insert blue image
    scrsz = get(0, 'Screensize');
    figure('Position', [scrsz(1) scrsz(2) scrsz(3) scrsz(4)]), imagesc(imnorm(temp));
    axis image;
    axis off; colormap(gray);
    
    % Ask user whether they want to change position of center of fovea
    choice = questdlg('Does the fovea position require adjustment?','ADJUST FOVEA LOCATION?','YES','NO','NO');
    switch choice
        case 'YES'
            imagesc(imnorm(temp));
            title('Please select a suitable point for the fovea.');
            axis image;
            axis off; colormap(gray);
            [fov_col, fov_row] = ginput(1);
            fovRow = round(fov_row);
            fovCol = round(fov_col);
            macRegionChoice = 'Yes';
        case 'NO'
            macRegionChoice = 'No';
    end
    
    % SELECT OPTIC DISK
    % Have user select closest point on optic disk to fovea.
    temp = blueAvgIm;
    temp(fovRow-2:fovRow+2,fovCol-2:fovCol+2) = 1;
    imagesc(imnorm(temp));
    title('Please select the point on the optic disk closest to the fovea');
    axis image;
    axis off; colormap(gray);
    [opDiskCol, opDiskRow] = ginput(1);
    close;
    
    % DEFINE MACULAR REGION (ESTIMATED TO BE 4 DEGREES OFF OF VISUAL AXIS)
    
    opDiskRadius = norm([fovRow - opDiskRow, fovCol - opDiskCol]);
    opDiskRadius = floor(opDiskRadius);
    
    % We are interested in the region 4 degrees from the fovea, so set up the
    % radius 4 degrees from the fovea. 4/12 is the conversion from pixel to
    % degree.
    macRadius = round((4/12)*opDiskRadius);    
    [patientPath patientID] = fileparts([mainDir filesep sprintf('%s', patientFolders(i).name)]);
    
    % Store fovRow, fovCol, opDiskRow, opDiskCol, opDiskRadius, and macRadius
    tempData = {num2str(patientID), fovRow, fovCol, macRadius, opDiskRow, opDiskCol, opDiskRadius};
    dataCenter = [dataCenter; tempData];
end

%% Write out data
cd(infoStorage)
xlswrite(centersExcelName, dataCenter)